package api

import (
	"fmt"
	"gateway/basic/config"
	goods "gateway/basic/proto/goodsrpc"
	"gateway/handler/request"
	"gateway/handler/response"
	"github.com/gin-gonic/gin"
)

// CalculateShippingFee
// @Summary      计算运费
// @Description  计算运费
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        user  query      request.CalculateShippingFeeReq  true  "订单参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /goods/calculateShippingFee [get]
func CalculateShippingFee(c *gin.Context) {
	var req request.CalculateShippingFeeReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	res, err := config.GoodsClient.CalculateShippingFee(c, &goods.CalculateShippingFeeReq{
		Weight:  req.Weight,
		WayLong: req.WayLong,
	})
	if err != nil {
		response.SrvError(c, "计算运费失败", err.Error())
		return
	}
	response.Success(c, "计算成功", res)
}

// BatchShipping
// @Summary      批量发货
// @Description  批量处理订单发货
// @Tags         商品模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.BatchShipping  true  "批量发货参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /goods/batchShipping [post]
func BatchShipping(c *gin.Context) {
	var req request.BatchShipping
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	var num int
	for _, v := range req.OrderIds {
		_, err := config.GoodsClient.BatchShipping(c, &goods.BatchShippingReq{
			OrderId: v,
			ShopId:  req.ShopId,
		})
		if err != nil {
			response.SrvError(c, fmt.Sprintf("订单id为%v的订单发货失败", v), err.Error())
			num++
		}
	}
	if num != 0 {
		response.ParameterError(c, "批量发货失败", nil)
		return
	}
	response.Success(c, "批量发货成功", nil)
}

// GoodsDetail
// @Summary      商品详情
// @Description  获取商品详细信息
// @Tags         商品模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.GetId  true  "商品ID参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /goods/detail [post]
func GoodsDetail(c *gin.Context) {
	var req request.GetId
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	res, err := config.GoodsClient.GoodsDetail(c, &goods.GoodsDetailReq{Id: int32(req.Id)})
	if err != nil {
		response.SrvError(c, "查询商品详情失败", err.Error())
		return
	}
	response.Success(c, "查询成功", res)
}

// GoodsOrderDetail
// @Summary      商品订单详情
// @Description  获取商品订单详细信息
// @Tags         商品模块
// @Accept       json
// @Produce      json
// @Param        user  body      request.GetId  true  "订单ID参数"
// @Success      200  {object}  response.Response
// @Failure      400   {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /goods/orderDetail [post]
func GoodsOrderDetail(c *gin.Context) {
	var req request.GetId
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	res, err := config.GoodsClient.OrderDetail(c, &goods.OrderDetailReq{OrderId: req.Id})
	if err != nil {
		response.SrvError(c, "查询订单失败", err.Error())
		return
	}
	response.Success(c, "查询成功", res)
}

func CreatCabinet(c *gin.Context) {
	var req request.CreateCabinet
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	res, err := config.GoodsClient.CreateCabinet(c, &goods.CreateCabinetReq{
		CabinetNo:        req.CabinetNo,
		ChargingStandard: req.ChargingStandard,
		AddressId:        req.AddressId,
		SizeId:           req.SizeId,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "添加成功", res)
}

func UpdateCabinet(c *gin.Context) {
	var req request.UpdateCabinet
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	res, err := config.GoodsClient.UpdateCabinet(c, &goods.UpdateCabinetReq{
		Id:               uint32(req.Id),
		CabinetNo:        req.CabinetNo,
		ChargingStandard: req.ChargingStandard,
		AddressId:        req.AddressId,
		SizeId:           req.SizeId,
		Status:           req.Id,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "修改成功", res)
}

func CabinetDetail(c *gin.Context) {
	var req request.GetId
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	res, err := config.GoodsClient.CabinetDetail(c, &goods.CabinetDetailReq{Id: uint32(req.Id)})
	if err != nil {
		response.ParameterError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "查询成功", res)
}

func CabinetLocationList(c *gin.Context) {
	res, err := config.GoodsClient.CabinetLocation(c, &goods.CabinetLocationReq{})
	if err != nil {
		response.SrvError(c, "服务端失败", err.Error())
		return
	}
	response.Success(c, "查询成功", res)
}

func GetCabinetBySizeOrAddress(c *gin.Context) {
	var req request.GetCabinetBySizeReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	res, err := config.GoodsClient.GetCabinetBySize(c, &goods.GetCabinetBySizeReq{
		SizeId:    req.SizeId,
		AddressId: req.AddressId,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "查询成功", res)
}
